﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.Unity;
using ePlanet.SingleWell.Models;
using ePlanet.SingleWell.Services;
using ePlanet.ResourceRequest.DataAccess;
using ePlanet.ResourceRequest.Models;
using ePlanet.ResourceRequest.Services;

namespace Web.Views.ResourceRequestManage
{
    public partial class ResourceRequest : PageBase
    {
        [Dependency]
        public IResourceRequestService m_IResourceRequestService
        {
            get;
            set;
        }

        [Dependency]
        public ISingleWellService m_ISingleWellService
        {
            get;
            set;
        }

        [Dependency]
        public IWellCatalogService m_IWellCatalogService
        {
            get;
            set;
        }

        private string m_DataID
        {
            get;
            set;
        }

        private Web.SystemLogin.Models.User m_CurrentUser
        {
            get
            {
                return (Web.SystemLogin.Models.User)Session["CurrentUser"];
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(Request.QueryString["DataID"]))
                {
                    throw new Exception("参数DataID不能为空");
                }
                else
                {
                    this.m_DataID = Request.QueryString["DataID"];
                }

                if (!IsPostBack)
                {
                    this.BindData();
                }
            }
            catch (Exception ex)
            {
                Alert(ex.Message);
            }
        }

        protected void btnApplyNewRequest_Click(object sender, EventArgs e)
        {
            try
            {
                if (!this.HasNoExpiredRequest())
                {
                    this.ApplyNewRequest();
                    string wellType = string.Empty;
                    if (string.IsNullOrEmpty(Request.QueryString["wellType"]))
                    {
                        wellType = this.m_IWellCatalogService.GetBaseCatalog().WELLCATALOG[0].WELLCATALOGID;
                    }
                    else
                    {
                        wellType = Request.QueryString["wellType"];
                    }
                    //string url = string.Format("ResourceRequestWellList.aspx?wellType{0}", wellType);
                    //MyAlert("提交申请成功", url);
                    Response.Write("<script>alert('提交申请成功');window.history.go(-2);</script>");
                }
                else
                {
                    Alert("此数据已经有未过期的合法申请，请仔细查看历史申请");
                }
            }
            catch (Exception ex)
            {
                Alert(ex.Message);
            }
        }

        #region 私有方法
        private void BindData()
        {
            SingleWellModel model = this.m_ISingleWellService.GetSingleWellModel(this.m_DataID);
            if (model != null && model.SINGLEWELL.Rows.Count > 0)
            {
                //绑定数据
                SingleWellModel.SINGLEWELLRow row = model.SINGLEWELL[0];
                if (!row.IsA0Null())
                {
                    this.txtA0.Text = row.A0.ToString();
                }
                if (!row.IsA3Null())
                {
                    this.txtA3.Text = row.A3.ToString();
                }
                if (!row.IsA4Null())
                {
                    this.txtA4.Text = row.A4.ToString();
                }

                if (!row.IsBEGINDATENull())
                {
                    this.txtBEGINDATE.Text = row.BEGINDATE.ToShortDateString();
                }
                if (!row.IsBEGINWORKDATENull())
                {
                    this.txtBeginWorkDate.Text = row.BEGINWORKDATE.ToShortDateString();
                }
                if (!row.IsDATAFILENAMENull())
                {
                    this.txtDatafileName.Text = row.DATAFILENAME;
                }
                if (!row.IsFINISHDATENull())
                {
                    this.txtFINISHDATE.Text = row.FINISHDATE.ToShortDateString();
                }
                if (!row.IsFINISHWORKDATENull())
                {
                    this.txtFinishWorkDate.Text = row.FINISHWORKDATE.ToShortDateString();
                }

                if (!row.IsWELLDEPTHNull())
                {
                    this.txtWellDepth.Text = row.WELLDEPTH.ToString();
                }
                if (!row.IsWELLTYPENull())
                {
                    this.txtWellType.Text = row.WELLTYPE;
                }
                if (!row.IsWORKMODENull())
                {
                    this.txtWorkMode.Text = row.WORKMODE;
                }
                if (!string.IsNullOrEmpty(row.WELLCODE))
                {
                    this.txtWellCode.Text = row.WELLCODE;
                }
            }
        }

        private bool HasNoExpiredRequest()
        {
            return this.m_IResourceRequestService.HasNoExpiredRequest(this.m_DataID, this.m_CurrentUser.User_ID);
        }

        private void ApplyNewRequest()
        {
            if (!string.IsNullOrEmpty(this.txtValidPeriod.Text.Trim()))
            {
                DateTime validPeriod = DateTime.MinValue;
                if (DateTime.TryParse(this.txtValidPeriod.Text.Trim(), out validPeriod))
                {
                    if (validPeriod.Date <= DateTime.Now.Date)
                    {
                        throw new Exception("申请期限必须大于当前日期");
                    }
                    ResourceRequestModel model = new ResourceRequestModel();
                    ResourceRequestModel.RESOURCEREQUESTRow row = model.RESOURCEREQUEST.NewRESOURCEREQUESTRow();

                    row.ID = Guid.NewGuid().ToString();
                    row.STATUSID = (decimal)ResourceRequestEnum.NewRequest;
                    row.TEMPUSERID = m_CurrentUser.User_ID;
                    row.VALIDPERIOD = validPeriod;
                    row.WELLDATAID = this.m_DataID;
                    row.CANDOWNLOAD = this.cbCanDownload.Checked == true ? 1 : 0;
                    row.CANVIEW = this.cbCanView.Checked == true ? 1 : 0;
                    model.RESOURCEREQUEST.AddRESOURCEREQUESTRow(row);
                    this.m_IResourceRequestService.ApplyNewRequest(model);
                }
                else
                {
                    throw new Exception("过期日期不是时间格式");
                }
            }
            else
            {
                throw new Exception("请输入过期时间");
            }
        }

        private void MyAlert(string msg, string url)
        {
            this.Response.Write("<script>alert(\"" + msg + "\");window.location.href=\"" + url + "\"</script>");
        }
        #endregion
    }
}